package com.zbvc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zbvc.dao.AdminDao;
import com.zbvc.entity.Admin;
import com.zbvc.util.DBUtil;

public class AdminDaoImpl implements AdminDao {
	/**
	 * 用户注册
	 */
	@Override
	public int reg(Admin admin) {
		String sql = 
				"INSERT INTO admin (aid,username,name,password,email,phone,status,lend_num,max_num) VALUES (null,?,?,?,?,?,?,?,?)";
		Connection conn = DBUtil.getConnectDb();
		int row=0;
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, admin.getUsername());
			ps.setString(2, admin.getName());
			ps.setString(3, admin.getPassword());
			ps.setString(4, admin.getEmail());
			ps.setString(5, admin.getPhone());
			ps.setInt(6,1);
			ps.setInt(7, 100);
			ps.setInt(8, 100);
			row = ps.executeUpdate();
		    return row;
		} catch (SQLException e) {
			System.out.println("注册异常");
			e.printStackTrace();
		}
		
		return row;
	}

	@Override
	public boolean login(String username, String password) {
		String sql = "SELECT * FROM admin WHERE username=? AND password=?";
		Connection conn = DBUtil.getConnectDb();
		ResultSet rs = null;
		PreparedStatement ps=null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if(rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBUtil.CloseDB(rs, ps, conn);
		}
		
		return false;
	}

	@Override
	public Admin getAdminInfo(String username, String password) {
		Admin admin = new Admin();
		String sql = "SELECT * FROM admin WHERE username=? AND password=?";
		Connection conn = DBUtil.getConnectDb();
		ResultSet rs = null;
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if(rs.next()) {
				admin.setAid(rs.getInt("aid"));
				admin.setUsername(rs.getString("username"));
				admin.setName(rs.getString("name"));
				admin.setPassword(rs.getString("password"));
				admin.setEmail(rs.getString("email"));
				admin.setPhone(rs.getString("phone"));
				admin.setStatus(rs.getInt("status"));
				admin.setLend_num(rs.getInt("lend_num"));
				admin.setMax_num(rs.getInt("max_num"));
			}
			return admin;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public Admin getAdminByAid(int aid) {
		Admin adminbean = new Admin();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from admin where aid="+aid;
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			if(rs.next()){
				adminbean.setAid(rs.getInt("aid"));
				adminbean.setUsername(rs.getString("username"));
				adminbean.setName(rs.getString("name"));
				adminbean.setPassword(rs.getString("password"));
				adminbean.setEmail(rs.getString("email"));
				adminbean.setPhone(rs.getString("phone"));
				adminbean.setStatus(rs.getInt("status"));
				adminbean.setLend_num(rs.getInt("lend_num"));
				adminbean.setMax_num(rs.getInt("max_num"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.CloseDB(rs, stm, conn);
		}
		return adminbean;
	}

	@Override
	public boolean updatePassword(int aid, String password) {
		Connection conn = DBUtil.getConnectDb();
		String sql = "UPDATE admin SET password=? WHERE aid=?";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, password);
			stm.setInt(2, aid);
			int row = stm.executeUpdate();
			if(row>0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.CloseDB(rs, stm, conn);
		}
		return false;
	}

	@Override
	public int updateAdminByAid(Admin admin) {
		String sql = "UPDATE admin SET name=?,email=?,phone=? WHERE aid=?";
		Connection conn = DBUtil.getConnectDb();
		PreparedStatement stm = null;
		ResultSet rs = null;
		int row = 0;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, admin.getName());
			stm.setString(2, admin.getEmail());
			stm.setString(3, admin.getPhone());
			stm.setInt(4, admin.getAid());
			row = stm.executeUpdate();
			return row;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}

}
